package com.yy.base.util;

import java.util.HashMap;
import java.util.Map;
import com.mes.entity.User;
import com.yy.base.page.Page;

/**
 * session容器
 * 每个session都是一个线程，分页类容放到线程中，能在线程中的每个位置拿到
 */
@SuppressWarnings("unchecked")
public class SessionInfo {
	public static String USER = "USER";
	public static String PAGE = "page";
	private static ThreadLocal local = new ThreadLocal();
	
	public static User getUser() {
		Map map = (Map)local.get();
		if (map == null)
			return null;
		return (User)map.get(USER);
	}
	
	public static void setUser(User user) {
		addParamenter(USER, user);
	}
	
	private static void addParamenter(String key, Object obj) {
		Map map = (Map)local.get();
		if (map == null) {
			map = new HashMap();
			local.set(map);
		}
		map.put(key, obj);
	}
	
	public static Page getPage() {
		Map map = (Map)local.get();
		Page page = null;
		if (map != null) {
			page = (Page)map.get(PAGE);
		}
		if (page == null) {
			page = new Page();
			addParamenter(PAGE, page);
		}
		return page;
	}
	
	public void remove() {
		local.remove();
	}
	/**
	 * 获取分页页面大小
	 */
	public static int getPageSize() {
		return SessionInfo.getPage().getPageSize();
	}

	/**
	 * 获取分页当前页面
	 */
	public static int getPageIndex() {
		return SessionInfo.getPage().getPageIndex();
	}
	/**
	 * 设置分页页面总数量
	 */
	public static void setTotal(long total) {
		SessionInfo.getPage().setTotal(total);
	}
}